package com.banksoft.cps.util;


import com.banksoft.cps.bean.MonitorValueImport;
import org.apache.commons.lang.StringUtils;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.CellType;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;

import java.io.FileInputStream;
import java.io.InputStream;
import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
import java.util.stream.Stream;

/**
 * Created by rongzhao.zhu on 2018/5/22.
 */
public class ExcelUtil {
    public static final String  emptyStr = "";
    public static final String  STRING = "String";
    public static final String  INTEGER = "Integer";
    public static final String  FLOAT = "Float";
    public static final String  DOUBLE = "Double";
    public static final String  BOOLEAN = "Boolean";


    public static <T> List<T> read(String path,Class<T> clazz,Map<Integer,String> indexMap) throws Exception{
        List<T> result  = new ArrayList<>();
        InputStream is = new FileInputStream(path);
        XSSFWorkbook xssfWorkbook = new XSSFWorkbook(is);
        XSSFSheet sheet = xssfWorkbook.getSheet("净积流量");

        int rowNum = sheet.getLastRowNum();
        for(int r=1;r<=rowNum;r++){
            Row row = sheet.getRow(r);
            if(row == null){
               continue;
            }else {
                Map<Integer,String> indexValue =   getRowContent(row);
                T obj = convert(clazz,indexValue,indexMap);
                result.add(obj);
            }
        }
        return result;
    }


    /**
     * 获取表格中的内容
     * 暂时只支持文字
     * @param row
     * @return
     */
    private static Map<Integer,String> getRowContent(Row row){
        Map<Integer,String> valueMap = new HashMap<>();
        int column = row.getPhysicalNumberOfCells();
        for(int c=0;c<column;c++){
            Cell ce = row.getCell(c);
            if(ce == null){
                continue;
            }else{
                String value = ce.getStringCellValue();

                valueMap.put(c,value);
            }
        }
        return valueMap;
    }
    private static <T> T convert(Class<T> tClass,Map<Integer,String> valueMap,Map<Integer,String> indexMap)
            throws Exception{
        T obj = tClass.newInstance();
        valueMap.keySet().stream().forEach(key->{
            try {
                String value = valueMap.get(key);
                String fieldName =  indexMap.get(key);
                if(StringUtils.isNotBlank(fieldName)){
                    Field field = tClass.getDeclaredField(fieldName);
                    field.setAccessible(true);
                    String fieldType = field.getType().getSimpleName();
                    if(fieldType.equals(STRING)){
                        field.set(obj,value);
                    }else if(fieldType.equals(INTEGER)){
                        field.set(obj,Integer.valueOf(value));
                    }else if(fieldType.equals(DOUBLE)){
                        field.set(obj,Double.valueOf(value));
                    }else if(fieldType.equals(FLOAT)){
                        field.set(obj,Float.valueOf(value));
                    }else if(fieldType.equals(BOOLEAN)){
                        field.set(obj,Boolean.valueOf(value));
                    }
                }

            } catch (Exception e) {
                e.printStackTrace();
            }

        });
        return obj;
    }
    public  static void main(String[] args){
        String s =   "121.121".split("\\.")[0];
        System.out.print(String.format("12.12", "d%"));
        System.out.print(System.currentTimeMillis());
        List<Map> doubles = Stream.of(new Double[]{12.1, 12.10, 12.2,11.11,11.0}).distinct().sorted().map(e->{
            Map map = new HashMap<>();
            map.put(e,"");
        return map;}).
                collect(Collectors.toList());
        Map<Integer,String> keyMap = new HashMap<>();
        keyMap.put(0,"monitorTimeStr");
        keyMap.put(1,"value");


        try {
           List<MonitorValueImport> list =   ExcelUtil.read("d:/test/3ad6ce58-e45f-446c-9381-eb4bd053aae5.xlsx",
                   MonitorValueImport.class,keyMap);
           System.out.print("ox");
        } catch (Exception e) {
            e.printStackTrace();
        }
    }






}
